package org.ftc.dao;

import java.util.List;

import org.ftc.domain.Committee;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class CommitteeDao extends CurrentDao <Committee, String> {

	@Override
	public Committee load(String id) {
		return super.load(id);
	}

	@Override
	public Committee get(String id) {
		return super.get(id);
	}

	@Override
	public synchronized boolean save(Committee entity) {
		return super.save(entity);
	}

	@Override
	public synchronized boolean saveOrUpdate(Committee entity) {
		return super.saveOrUpdate(entity);
	}

	@Override
	public synchronized boolean update(Committee entity) {
		return super.update(entity);
	}

	@Override
	public synchronized boolean delete(Committee entity) {
		return super.delete(entity);
	}

	@Override
	public synchronized boolean delete(String id) {
		return super.delete(id);
	}
	
	@Override
	public List<Committee> findPage(int offset, int pageSize) {
		return super.findPage(offset, pageSize);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Committee> findAll() {
		try{	
			Criteria criteria = getSession().createCriteria(Committee.class); 
			criteria.addOrder(Order.asc("sort"));
			return (List<Committee>) criteria.list();
		}catch( Exception e ){
			getLog().error(e.toString());
			return null; 
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Committee> findCkd() {
		try{	
			Criteria criteria = getSession().createCriteria(Committee.class); 
			criteria.createAlias("emMapping", "ctMap");
			criteria.createAlias("emMapping.committeeTeam", "comTeam");
			criteria.add(Restrictions.eq("comTeam.enable", true));
			criteria.addOrder(Order.asc("ctMap.sort"));
			return (List<Committee>) criteria.list();
		}catch( Exception e ){
			getLog().error(e.toString());
			return null; 
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Committee> findByCode(String code) {
		try{	
			Criteria criteria = getSession().createCriteria(Committee.class); 
			criteria.createAlias("emMapping", "ctMap");
			criteria.createAlias("emMapping.committeeTeam", "comTeam");
			criteria.add(Restrictions.eq("comTeam.tCode", code));
			criteria.addOrder(Order.asc("ctMap.sort"));
			return (List<Committee>) criteria.list();
		}catch( Exception e ){
			getLog().error(e.toString());
			return null; 
		}
	}

	@Override
	public long countAll() {
		return super.countAll();
	}


}
